package com.hook3.pianke.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.*;
import android.os.Process;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.hook3.pianke.R;

/**
 * 设置页面
 * Created by HOME on 2015/12/8.
 */
public class SetingActivity {

    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;
    Intent intent;
    private View  scrollView;
    private RelativeLayout set_auto_start_layout;//定时开启
    private RelativeLayout set_auto_close_layout;//定时关闭
    private   RelativeLayout  set_common_layout;//通用设置
    private   RelativeLayout  set_about_layout;//关于片刻
    private   RelativeLayout  set_feedback_layout;//意见反馈
    private   RelativeLayout  set_logout_layout;//登录片刻
    private   RelativeLayout  set_close_layout;//退出应用

    public  SetingActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
    }

    /**
     * 实例化自己要实现的内容
     */
    public void init() {
        scrollView = View.inflate(activity, R.layout.scrollview_set,null);
        frameLayout.addView(scrollView);
        frameLayout.setTag(scrollView);
        initView();

    }

    /**
     * 初始化和设置监听
     */
    private void initView() {
        set_auto_start_layout = (RelativeLayout) scrollView.findViewById(R.id.set_auto_start_layout);
        set_auto_close_layout = (RelativeLayout) scrollView.findViewById(R.id.set_auto_close_layout);
        set_common_layout = (RelativeLayout) scrollView.findViewById(R.id.set_common_layout);
        set_about_layout = (RelativeLayout) scrollView.findViewById(R.id.set_about_layout);
        set_feedback_layout = (RelativeLayout) scrollView.findViewById(R.id.set_feedback_layout);
        set_logout_layout = (RelativeLayout) scrollView.findViewById(R.id.set_logout_layout);
        set_close_layout = (RelativeLayout) scrollView.findViewById(R.id.set_close_layout);


        set_auto_start_layout.setOnClickListener(new MySettingOnClickLinear());
        set_auto_close_layout.setOnClickListener(new MySettingOnClickLinear());
        set_common_layout.setOnClickListener(new MySettingOnClickLinear());
        set_about_layout.setOnClickListener(new MySettingOnClickLinear());
        set_feedback_layout.setOnClickListener(new MySettingOnClickLinear());

        set_logout_layout.setOnClickListener(new MySettingOnClickLinear());
        set_close_layout.setOnClickListener(new MySettingOnClickLinear());

    }

    /**
     * 点击监听
     */
    private class MySettingOnClickLinear implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.set_auto_start_layout:
                    Toast.makeText(activity, "进入定时开启", Toast.LENGTH_SHORT).show();
                     intent = new Intent(activity,AutoStart.class);
                    activity.startActivity(intent);

                    break;
                case R.id.set_auto_close_layout:
                    Toast.makeText(activity, "进入定时关闭Ting", Toast.LENGTH_SHORT).show();

                    break;
                case R.id.set_common_layout:
                     intent = new Intent(activity,CommonSetting.class);
                    activity.startActivity(intent);

                    break;
                case R.id.set_about_layout:
                    intent = new Intent(activity,AboutPianKe.class);
                    activity.startActivity(intent);

                    break;
                case R.id.set_feedback_layout:
                    Toast.makeText(activity, "进入意见反馈", Toast.LENGTH_SHORT).show();


                    break;
                case R.id.set_logout_layout:
                     intent = new Intent(activity,RegisterActivity.class);
                    activity.startActivity(intent);

                    break;
                case R.id.set_close_layout:
                    new AlertDialog.Builder(activity)
                       .setTitle("温馨提示")
                       .setMessage("你确定要退出应用吗？")
                       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                           @Override
                           public void onClick(DialogInterface dialog, int which) {
                               Intent intent = new Intent(Intent.ACTION_MAIN);
                               intent.addCategory(Intent.CATEGORY_HOME);
                               intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                               activity.startActivity(intent);
                               android.os.Process.killProcess(Process.myPid());

                           }
                       })
                        .setNegativeButton("取消", null)
                            .show();
                default:
                    break;
            }

        }
    }
}
